<!-- build time:Tue Jul 21 2020 12:41:23 GMT+0800 (GMT+08:00) --><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 4.2.0"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png"><link rel="icon" type="image/png" sizes="32x32" href="/img/blog_favicon/favicon-32x32-next.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/blog_favicon/favicon-16x16-next.png"><link rel="mask-icon" href="/images/logo.svg" color="#222"><meta name="baidu-site-verification" content="r15J5zOS2KI43yBS"><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css"><script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script><script id="hexo-configurations">var NexT=window.NexT||{},CONFIG={hostname:new URL("http://www.zhengjiaao.cn").hostname,root:"/",scheme:"Muse",version:"7.6.0",exturl:!1,sidebar:{position:"left",display:"post",padding:18,offset:12,onmobile:!1},copycode:{enable:!0,show_result:!0,style:null},back2top:{enable:!0,sidebar:!1,scrollpercent:!0},bookmark:{enable:!0,color:"#222",save:"auto"},fancybox:!0,mediumzoom:!1,lazyload:!1,pangu:!1,comments:{style:"tabs",active:"valine",storage:!0,lazyload:!1,nav:null,activeClass:"valine"},algolia:{appID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}},localsearch:{enable:!0,trigger:"auto",top_n_per_article:1,unescape:!1,preload:!1},path:"search.xml",motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}}}</script><meta name="description" content="之前有写过springboot+redis实战,这里不做redis缓存测试了下方有springboot+ehcache的测试用例目录："><meta property="og:type" content="article"><meta property="og:title" content="ehcache、redis、memcached缓存的应用场景"><meta property="og:url" content="http://www.zhengjiaao.cn/posts/477243062.html"><meta property="og:site_name" content="宇宙小神特别萌"><meta property="og:description" content="之前有写过springboot+redis实战,这里不做redis缓存测试了下方有springboot+ehcache的测试用例目录："><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-a86397401e3d2e29.png"><meta property="article:published_time" content="2020-01-03T08:14:50.000Z"><meta property="article:modified_time" content="2020-07-20T10:37:55.305Z"><meta property="article:author" content="宇宙小神特别萌"><meta property="article:tag" content="SpringBoot"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/15645795-a86397401e3d2e29.png"><link rel="canonical" href="http://www.zhengjiaao.cn/posts/477243062.html"><script id="page-configurations">CONFIG.page={sidebar:"",isHome:!1,isPost:!0}</script><title>ehcache、redis、memcached缓存的应用场景 | 宇宙小神特别萌</title><noscript><style>.sidebar-inner,.use-motion .brand,.use-motion .collection-header,.use-motion .comments,.use-motion .menu-item,.use-motion .pagination,.use-motion .post-block,.use-motion .post-body,.use-motion .post-header{opacity:initial}.use-motion .site-subtitle,.use-motion .site-title{opacity:initial;top:initial}.use-motion .logo-line-before i{left:initial}.use-motion .logo-line-after i{right:initial}</style></noscript><link rel="alternate" href="/atom.xml" title="宇宙小神特别萌" type="application/atom+xml"></head><body itemscope itemtype="http://schema.org/WebPage"><div class="container use-motion"><div class="headband"></div><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-meta custom-logo"><div class="site-meta-headline"><a><img class="custom-logo-image" src="/img/blog_favicon/favicon-32x32-next.png" alt="宇宙小神特别萌"></a></div><div><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">宇宙小神特别萌</span> <span class="logo-line-after"><i></i></span></a></div><p class="site-subtitle">ZhengJa</p></div><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">27</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">116</span></a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a></li><li class="menu-item menu-item-messageboard"><a href="/messageboard/" rel="section"><i class="fa fa-fw fa-address-book"></i>留言板</a></li><li class="menu-item menu-item-links"><a href="/links/" rel="section"><i class="fa fa-fw fa-user-plus"></i>友链</a></li><li class="menu-item menu-item-shuoshuo"><a href="/shuoshuo/" rel="section"><i class="fa fa-fw fa-file-text-o"></i>说说</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="site-search"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocorrect="off" autocapitalize="none" placeholder="搜索..." spellcheck="false" type="text" id="search-input"></div><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span></div><div id="search-result"></div></div><div class="search-pop-overlay"></div></div></div></header><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span>0%</span></div><div class="reading-progress-bar"></div><a role="button" class="book-mark-link book-mark-link-fixed"></a> <a href="https://github.com/zhengjiaao" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><main class="main"><div class="main-inner"><div class="content-wrap"><div class="content"><div class="posts-expand"><article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN"><link itemprop="mainEntityOfPage" href="http://www.zhengjiaao.cn/posts/477243062.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/my-images/avatar.gif"><meta itemprop="name" content="宇宙小神特别萌"><meta itemprop="description" content="人生就是一次幸运的旅行！"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="宇宙小神特别萌"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">ehcache、redis、memcached缓存的应用场景</h1><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间：2020-01-03 16:14:50" itemprop="dateCreated datePublished" datetime="2020-01-03T16:14:50+08:00">2020-01-03</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-check-o"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间：2020-07-20 18:37:55" itemprop="dateModified" datetime="2020-07-20T18:37:55+08:00">2020-07-20</time> </span><span id="/posts/477243062.html" class="post-meta-item leancloud_visitors" data-flag-title="ehcache、redis、memcached缓存的应用场景" title="热度"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">热度：</span> <span class="leancloud-visitors-count"></span> <span>℃</span> </span><span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">热度：</span> <span id="busuanzi_value_page_pv"></span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-comment-o"></i> </span><span class="post-meta-item-text">Valine：</span> <a title="valine" href="/posts/477243062.html#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/posts/477243062.html" itemprop="commentCount"></span></a></span><br><span class="post-meta-item" title="本文字数"><span class="post-meta-item-icon"><i class="fa fa-file-word-o"></i> </span><span class="post-meta-item-text">本文字数：</span> <span>15k</span> </span><span class="post-meta-item" title="阅读时长"><span class="post-meta-item-icon"><i class="fa fa-clock-o"></i> </span><span class="post-meta-item-text">阅读时长 &asymp;</span> <span>14 分钟</span></span></div></header><div class="post-body" itemprop="articleBody"><meta name="referrer" content="no-referrer"><ul><li>之前有写过<a href="https://www.jianshu.com/p/5596c3a4978d" target="_blank" rel="noopener">springboot+redis实战</a>,这里不做redis缓存测试了</li><li>下方有springboot+ehcache的测试用例<br>目录：<br><img src="https://upload-images.jianshu.io/upload_images/15645795-a86397401e3d2e29.png" alt="应用场景目录.png"></li></ul><a id="more"></a><h3 id="应用场景："><a href="#应用场景：" class="headerlink" title="应用场景："></a>应用场景：</h3><h4 id="1-ehcache"><a href="#1-ehcache" class="headerlink" title="1.ehcache"></a>1.ehcache</h4><p>ehcache直接在jvm虚拟机中缓存 ，速度快 ，效率高；但是缓存共享麻烦 ，集群分布式应用不方便。</p><h4 id="2-redis"><a href="#2-redis" class="headerlink" title="2.redis"></a>2.redis</h4><p>redis是通过socket访问到缓存服务 ，效率比ecache低 ，比数据库要快很多 ，处理集群和分布式缓存方便 ，有成熟的方案。</p><ul><li>如果是单个应用或者对缓存访问要求很高的应用 ，用ehcache。</li><li>如果是大型系统 ，存在缓存共享、分布式部署、缓存内容很大的 ，建议用redis。</li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">补充下：ehcache也有缓存共享方案 ，不过是通过RMI或者Jgroup多播方式进行广播缓存通知更新 ，缓存共享复杂 ，维护不方便；简单的共享可以 ，但是涉及到缓存恢复 ，大数据缓存 ，则不合适</span><br></pre></td></tr></table></figure><h4 id="3-memcached"><a href="#3-memcached" class="headerlink" title="3.memcached"></a>3.memcached</h4><p>是一种高性能、分布式对象缓存系统 ，最初设计于缓解动态网站<a href="http://lib.csdn.net/base/mysql" target="_blank" rel="noopener">数据库</a>加载数据的延迟性 ，你可以把它想象成一个大的内存HashTable ，就是一个key-value键值缓存。保存的对象并不是持久的 ，服务停止之后 ，里边的数据就会丢失。</p><h4 id="4-总结"><a href="#4-总结" class="headerlink" title="4.总结"></a>4.总结</h4><ul><li><p>redis和memcached相比的独特之处：<br>1、redis可以用来做存储(storage),而memcached是用来做缓存(cache) ，redis的速度比memcached快很多。<br>这个特点主要因为其有持久化功能<br>2、数据支持类型：redis中存储的数据有多种结构 ，而memcached存储的数据只有一种类型“字符串”<br>3、存储方式：Memecache把数据全部存在内存之中 ，断电后会挂掉 ，数据不能超过内存大小。Redis有部份存在硬盘上 ，这样能保证数据的持久性<br>4、使用底层模型不同：它们之间底层实现方式 以及与客户端之间通信的应用协议不一样。Redis直接自己构建了VM 机制 ，因为一般的系统调用系统函数的话 ，会浪费一定的时间去移动和请求<br>5、value大小：redis最大可以达到1GB ，而memcache只有1MB</p></li><li><p>redis和ehcache相比：</p><p>1、Redis：属于独立的运行程序 ，需要单独安装后 ，使用JAVA中的Jedis来操纵。因为它是独立 ，所以如果你写个单元测试程序 ，放一些数据在Redis中 ，然后又写一个程序去拿数据 ，那么是可以拿到这个数据的。<br>2、ehcache：与Redis明显不同 ，它与java程序是绑在一起的 ，java程序活着 ，它就活着。譬如 ，写一个独立程序放数据 ，再写一个独立程序拿数据 ，那么是拿不到数据的。只能在独立程序中才能拿到数据</p></li></ul><h4 id="5-仅仅演示springboot-ehcache代码："><a href="#5-仅仅演示springboot-ehcache代码：" class="headerlink" title="5.仅仅演示springboot+ehcache代码："></a>5.仅仅演示springboot+ehcache代码：</h4><h5 id="1、ehcache依赖："><a href="#1、ehcache依赖：" class="headerlink" title="1、ehcache依赖："></a>1、ehcache依赖：</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Spring Boot 缓存支持启动器 --&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;spring-boot-starter-cache&lt;&#x2F;artifactId&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line">   &lt;!-- Ehcache 坐标 --&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;net.sf.ehcache&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;ehcache&lt;&#x2F;artifactId&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure><p>具体的pom文件</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependencies&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;spring-boot-starter-web&lt;&#x2F;artifactId&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.projectlombok&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;lombok&lt;&#x2F;artifactId&gt;</span><br><span class="line">      &lt;optional&gt;true&lt;&#x2F;optional&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;spring-boot-starter-test&lt;&#x2F;artifactId&gt;</span><br><span class="line">      &lt;scope&gt;test&lt;&#x2F;scope&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line"></span><br><span class="line">   &lt;!--事务 @Transactional--&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.springframework&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;spring-tx&lt;&#x2F;artifactId&gt;</span><br><span class="line">      &lt;version&gt;5.1.8.RELEASE&lt;&#x2F;version&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line"></span><br><span class="line">   &lt;!--集成swagger-API--&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;io.swagger&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;swagger-models&lt;&#x2F;artifactId&gt;</span><br><span class="line">      &lt;!--&lt;scope&gt;compile&lt;&#x2F;scope&gt;--&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;io.swagger&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;swagger-annotations&lt;&#x2F;artifactId&gt;</span><br><span class="line">      &lt;!--&lt;scope&gt;compile&lt;&#x2F;scope&gt;--&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;io.springfox&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;springfox-swagger-ui&lt;&#x2F;artifactId&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;io.springfox&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;springfox-swagger2&lt;&#x2F;artifactId&gt;</span><br><span class="line">      &lt;exclusions&gt;</span><br><span class="line">         &lt;exclusion&gt;</span><br><span class="line">            &lt;groupId&gt;io.swagger&lt;&#x2F;groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;swagger-models&lt;&#x2F;artifactId&gt;</span><br><span class="line">         &lt;&#x2F;exclusion&gt;</span><br><span class="line">         &lt;exclusion&gt;</span><br><span class="line">            &lt;groupId&gt;io.swagger&lt;&#x2F;groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;swagger-annotations&lt;&#x2F;artifactId&gt;</span><br><span class="line">         &lt;&#x2F;exclusion&gt;</span><br><span class="line">      &lt;&#x2F;exclusions&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line"></span><br><span class="line">   &lt;!-- Spring Boot 缓存支持启动器 --&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;spring-boot-starter-cache&lt;&#x2F;artifactId&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line">   &lt;!-- Ehcache 坐标 --&gt;</span><br><span class="line">   &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;net.sf.ehcache&lt;&#x2F;groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;ehcache&lt;&#x2F;artifactId&gt;</span><br><span class="line">   &lt;&#x2F;dependency&gt;</span><br><span class="line"></span><br><span class="line">   &lt;&#x2F;dependencies&gt;</span><br></pre></td></tr></table></figure><h5 id="2、创建ehcache-xml文件"><a href="#2、创建ehcache-xml文件" class="headerlink" title="2、创建ehcache.xml文件"></a>2、创建ehcache.xml文件</h5><p>在resources下面创建ehcache.xml ，复制以下内容。</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line">&lt;ehcache xmlns:xsi&#x3D;&quot; http:&#x2F;&#x2F;www.w3.org&#x2F;2001&#x2F;XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation&#x3D;&quot;..&#x2F;config&#x2F;ehcache.xsd&quot;&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--这个是磁盘存储路径 ，当内存缓存满了的时候 ，就会往这里面放 ，java.io.tmpdir是操作系统缓存的临时目录 ，不同操作系统缓存目录不一样。--&gt;</span><br><span class="line">    &lt;diskStore path&#x3D;&quot;java.io.tmpdir&quot;&#x2F;&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--defaultCache:echcache的默认缓存策略  --&gt;</span><br><span class="line">    &lt;defaultCache</span><br><span class="line">            maxElementsInMemory&#x3D;&quot;10000&quot;</span><br><span class="line">            eternal&#x3D;&quot;false&quot;</span><br><span class="line">            timeToIdleSeconds&#x3D;&quot;120&quot;</span><br><span class="line">            timeToLiveSeconds&#x3D;&quot;120&quot;</span><br><span class="line">            maxElementsOnDisk&#x3D;&quot;10000000&quot;</span><br><span class="line">            diskExpiryThreadIntervalSeconds&#x3D;&quot;120&quot;</span><br><span class="line">            memoryStoreEvictionPolicy&#x3D;&quot;LRU&quot;&gt;</span><br><span class="line">        &lt;persistence strategy&#x3D;&quot;localTempSwap&quot;&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;defaultCache&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--测试：自定义缓存策略1--&gt;</span><br><span class="line">    &lt;cache name&#x3D;&quot;users&quot;</span><br><span class="line">           maxElementsInMemory&#x3D;&quot;10000&quot;</span><br><span class="line">           eternal&#x3D;&quot;false&quot;</span><br><span class="line">           timeToIdleSeconds&#x3D;&quot;120&quot;</span><br><span class="line">           timeToLiveSeconds&#x3D;&quot;120&quot;</span><br><span class="line">           maxElementsOnDisk&#x3D;&quot;10000000&quot;</span><br><span class="line">           diskExpiryThreadIntervalSeconds&#x3D;&quot;120&quot;</span><br><span class="line">           memoryStoreEvictionPolicy&#x3D;&quot;LRU&quot;&gt;</span><br><span class="line">        &lt;persistence strategy&#x3D;&quot;localTempSwap&quot;&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;cache&gt;</span><br><span class="line">    &lt;!--测试：自定义缓存策略2--&gt;</span><br><span class="line">    &lt;cache name&#x3D;&quot;users2&quot;</span><br><span class="line">           maxElementsInMemory&#x3D;&quot;10000&quot;</span><br><span class="line">           eternal&#x3D;&quot;false&quot;</span><br><span class="line">           timeToIdleSeconds&#x3D;&quot;120&quot;</span><br><span class="line">           timeToLiveSeconds&#x3D;&quot;120&quot;</span><br><span class="line">           maxElementsOnDisk&#x3D;&quot;10000000&quot;</span><br><span class="line">           diskExpiryThreadIntervalSeconds&#x3D;&quot;120&quot;</span><br><span class="line">           memoryStoreEvictionPolicy&#x3D;&quot;LRU&quot;&gt;</span><br><span class="line">        &lt;persistence strategy&#x3D;&quot;localTempSwap&quot;&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;cache&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;ehcache&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &lt;!--</span><br><span class="line">    maxElementsInMemory      内存缓存中最多可以存放的元素数量,若放入Cache中的元素超过这个数值,则有以下两种情况</span><br><span class="line">                             1)若overflowToDisk&#x3D;true,则会将Cache中多出的元素放入磁盘文件中</span><br><span class="line">                             2)若overflowToDisk&#x3D;false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素</span><br><span class="line">    overflowToDisk           内存不足时,是否启用磁盘缓存</span><br><span class="line">    eternal                  缓存中对象是否永久有效</span><br><span class="line">    timeToIdleSeconds        缓存数据在失效前的允许闲置时间(单位:秒) ，仅当eternal&#x3D;false时使用,默认值是0表示可闲置时间无穷大,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除</span><br><span class="line">    timeToLiveSeconds        缓存数据的总的存活时间（单位：秒） ，仅当eternal&#x3D;false时使用 ，从创建开始计时 ，失效结束。</span><br><span class="line">    maxElementsOnDisk        磁盘缓存中最多可以存放的元素数量,0表示无穷大</span><br><span class="line">    diskExpiryThreadIntervalSeconds   磁盘缓存的清理线程运行间隔,默认是120秒</span><br><span class="line">    memoryStoreEvictionPolicy   内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存  共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出)</span><br><span class="line"></span><br><span class="line">    另外 ，defaultCache是默认缓存方式 ，cache是自定义的缓存方式 ，自行设置name</span><br><span class="line"></span><br><span class="line">    --&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--timeToIdleSeconds 当缓存闲置n秒后销毁 --&gt;</span><br><span class="line">    &lt;!--timeToLiveSeconds 当缓存存活n秒后销毁 --&gt;</span><br><span class="line">    &lt;!-- 缓存配置</span><br><span class="line">        name:缓存名称。</span><br><span class="line">        maxElementsInMemory：缓存最大个数。</span><br><span class="line">        eternal:对象是否永久有效 ，一但设置了 ，timeout将不起作用。</span><br><span class="line">        timeToIdleSeconds：设置对象在失效前的允许闲置时间（单位：秒）。仅当eternal&#x3D;false对象不是永久有效时使用 ，可选属性 ，默认值是0 ，也就是可闲置时间无穷大。</span><br><span class="line">        timeToLiveSeconds：设置对象在失效前允许存活时间（单位：秒）。最大时间介于创建时间和失效时间之间。仅当eternal&#x3D;false对象不是永久有效时使用 ，默认是0. ，也就是对象存活时间无穷大。</span><br><span class="line">        overflowToDisk：当内存中对象数量达到maxElementsInMemory时 ，Ehcache将会对象写到磁盘中。 diskSpoolBufferSizeMB：这个参数设置DiskStore（磁盘缓存）的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。</span><br><span class="line">        maxElementsOnDisk：硬盘最大缓存个数。</span><br><span class="line">        diskPersistent：是否缓存虚拟机重启期数据 Whether the disk</span><br><span class="line">        store persists between restarts of the Virtual Machine. The default value</span><br><span class="line">        is false.</span><br><span class="line">        diskExpiryThreadIntervalSeconds：磁盘失效线程运行时间间隔 ，默认是120秒。  memoryStoreEvictionPolicy：当达到maxElementsInMemory限制时 ，Ehcache将会根据指定的策略去清理内存。默认策略是</span><br><span class="line">    LRU（最近最少使用）。你可以设置为FIFO（先进先出）或是LFU（较少使用）。</span><br><span class="line">        clearOnFlush：内存数量最大时是否清除。 --&gt;</span><br></pre></td></tr></table></figure><h5 id="3、参考ehcache-xml配置文件详解"><a href="#3、参考ehcache-xml配置文件详解" class="headerlink" title="3、参考ehcache.xml配置文件详解"></a>3、参考ehcache.xml配置文件详解</h5><p>​ diskStore：为缓存路径 ，ehcache分为内存和磁盘两级 ，此属性定义磁盘的缓存位置。<br>​ defaultCache：默认缓存策略 ，当ehcache找不到定义的缓存时 ，则使用这个缓存策略。只能定义一个。<br>​ name:缓存名称。<br>​ maxElementsInMemory:缓存最大数目<br>​ maxElementsOnDisk：硬盘最大缓存个数。<br>​ eternal:对象是否永久有效 ，一但设置了 ，timeout将不起作用。<br>​ overflowToDisk:是否保存到磁盘 ，当系统宕机时<br>​ timeToIdleSeconds:设置对象在失效前的允许闲置时间（单位：秒）。仅当eternal=false对象不是永久有效时使用 ，可选属性 ，默认值是0 ，也就是可闲置时间无穷大。<br>​ timeToLiveSeconds:设置对象在失效前允许存活时间（单位：秒）。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用 ，默认是0. ，也就是对象存活时间无穷大。<br>​ diskPersistent：是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.<br>​ diskSpoolBufferSizeMB：这个参数设置DiskStore（磁盘缓存）的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。<br>​ diskExpiryThreadIntervalSeconds：磁盘失效线程运行时间间隔 ，默认是120秒。<br>​ memoryStoreEvictionPolicy：当达到maxElementsInMemory限制时 ，Ehcache将会根据指定的策略去清理内存。默认策略是LRU（最近最少使用）。你可以设置为FIFO（先进先出）或是LFU（较少使用）。<br>​ clearOnFlush：内存数量最大时是否清除。<br>​ FIFO ，first in first out ，先进先出。<br>​ LFU ， Less Frequently Used ，一直以来最少被使用的。如上面所讲 ，缓存的元素有一个hit属性 ，hit值最小的将会被清出缓存。<br>​ LRU ，Least Recently Used ，最近最少使用的 ，缓存的元素有一个时间戳 ，当缓存容量满了 ，而又需要腾出地方来缓存新的元素的时候 ，那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。</p><h5 id="4、添加启用缓存"><a href="#4、添加启用缓存" class="headerlink" title="4、添加启用缓存"></a>4、添加启用缓存</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@EnableCaching  &#x2F;&#x2F;添加启用缓存</span><br></pre></td></tr></table></figure><p>***Application.java</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">@SpringBootApplication</span><br><span class="line">@EnableCaching  &#x2F;&#x2F;添加启用缓存</span><br><span class="line">public class SpringbootTestCacheApplication &#123;</span><br><span class="line"></span><br><span class="line">   public static void main(String[] args) &#123;</span><br><span class="line">      SpringApplication.run(SpringbootTestCacheApplication.class, args);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h5 id="5、EhcacheController-java"><a href="#5、EhcacheController-java" class="headerlink" title="5、EhcacheController.java"></a>5、EhcacheController.java</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.dist.entity.UserEntity;</span><br><span class="line"><span class="keyword">import</span> com.dist.service.UserService;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.Api;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.ApiOperation;</span><br><span class="line"><span class="keyword">import</span> io.swagger.annotations.ApiParam;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Qualifier;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**Ehcache 缓存</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/15 10:51</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Api</span>(tags = &#123;<span class="string">"EhcacheController"</span>&#125;,description = <span class="string">"Ehcache 缓存"</span>)</span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(value = <span class="string">"rest/ehcache"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">EhcacheController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="meta">@Qualifier</span>(value = <span class="string">"userServiceImpl"</span>)</span><br><span class="line">    <span class="keyword">private</span> UserService userService;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"保存缓存-users"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"getuserbyid"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">getUserById</span><span class="params">(@ApiParam(value = <span class="string">"查询字段：id"</span>)</span> @RequestParam Integer id)</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//缓存存放的目录</span></span><br><span class="line">        String userHome = System.getProperty(<span class="string">"user.home"</span>);</span><br><span class="line">        String userDir = System.getProperty(<span class="string">"user.dir"</span>);</span><br><span class="line">        String tmpDir = System.getProperty(<span class="string">"java.io.tmpdir"</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"userHome:"</span>+userHome);</span><br><span class="line">        System.out.println(<span class="string">"userDir:"</span>+userDir);</span><br><span class="line">        System.out.println(<span class="string">"tmpDir:"</span>+tmpDir);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.userService.getUserById(id);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"清除缓存-users"</span>,httpMethod = <span class="string">"POST"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"saveuser"</span>,method = RequestMethod.POST)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">saveUser</span><span class="params">(@ApiParam(value = <span class="string">"参考：Model"</span>)</span> @RequestBody UserEntity userEntity)</span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.userService.saveUser(userEntity);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"模拟数据库保存"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"save"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">save</span><span class="params">(@ApiParam(value = <span class="string">"typeId"</span>)</span> @RequestParam String typeId) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.userService.save(typeId);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"模拟数据库更新"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"update"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">update</span><span class="params">(@ApiParam(value = <span class="string">"typeId"</span>)</span> @RequestParam String typeId) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.userService.update(typeId);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"模拟数据库删除"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"delete"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">delete</span><span class="params">(@ApiParam(value = <span class="string">"typeId"</span>)</span> @RequestParam String typeId) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.userService.delete(typeId);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"模拟数据库查询"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"select"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">select</span><span class="params">(@ApiParam(value = <span class="string">"typeId"</span>)</span> @RequestParam String typeId) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.userService.select(typeId);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiOperation</span>(value = <span class="string">"复杂的缓存规则"</span>,httpMethod = <span class="string">"GET"</span>)</span><br><span class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"selectbyname"</span>,method = RequestMethod.GET)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserEntity <span class="title">selectByName</span><span class="params">(@ApiParam(value = <span class="string">"name"</span>)</span> @RequestParam String name) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.userService.selectByName(name);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h5 id="6、接口UserService-java"><a href="#6、接口UserService-java" class="headerlink" title="6、接口UserService.java"></a>6、接口UserService.java</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.dist.entity.UserEntity;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/15 10:36</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 测试 ehcache 保存缓存</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">UserEntity <span class="title">getUserById</span><span class="params">(Integer id)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 测试 ehcache 清楚缓存</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> userEntity</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">saveUser</span><span class="params">(UserEntity userEntity)</span></span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 以下为模拟数据库操作</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> typeId</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">String <span class="title">save</span><span class="params">(String typeId)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function">String <span class="title">update</span><span class="params">(String typeId)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function">String <span class="title">delete</span><span class="params">(String typeId)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function">String <span class="title">select</span><span class="params">(String typeId)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 复杂的缓存</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> name</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">UserEntity <span class="title">selectByName</span><span class="params">(String name)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h5 id="7、实现类UserServiceImpl-java"><a href="#7、实现类UserServiceImpl-java" class="headerlink" title="7、实现类UserServiceImpl.java"></a>7、实现类UserServiceImpl.java</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.service.Impl;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.dist.entity.UserEntity;</span><br><span class="line"><span class="keyword">import</span> com.dist.service.UserService;</span><br><span class="line"><span class="keyword">import</span> org.springframework.cache.annotation.*;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;</span><br><span class="line"><span class="keyword">import</span> org.springframework.transaction.annotation.Transactional;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/15 10:36</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Service</span>(value = <span class="string">"userServiceImpl"</span>)</span><br><span class="line"><span class="meta">@Transactional</span>     <span class="comment">//事务 ，表示该类下所有的都受事务控制</span></span><br><span class="line"><span class="meta">@CacheConfig</span>(cacheNames = <span class="string">"users2"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceImpl</span> <span class="keyword">implements</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 测试 ehcache 保存缓存</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Cacheable</span> 可以标记在一个方法上 ，也可以标记在一个类上</span></span><br><span class="line"><span class="comment">     * 流程：被调用后将其返回值缓存起来 ，以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果 ，而不需要再次执行该方法</span></span><br><span class="line"><span class="comment">     * 如何缓存：缓存方法的返回值时是以键值对进行缓存的 ，值就是方法的返回结果</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@Cacheable</span>可以指定三个属性 ，value、key和condition</span></span><br><span class="line"><span class="comment">     *  value属性指定cache的名称（即选择ehcache.xml中哪种缓存方式存储）</span></span><br><span class="line"><span class="comment">     *  key（默认策略）属性是用来指定Spring缓存方法的返回结果时对应的key的 ，该属性支持SpringEL表达式;可使用“#参数名”或者“#p参数index”</span></span><br><span class="line"><span class="comment">     *  condition 缓存的条件 ，可以为空 ，使用 SpEL 编写 ，返回 true 或者 false ，只有为 true 才进行缓存 如：condition = "#userName.length()&gt;2"</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">//@Cacheable(value = "users")</span></span><br><span class="line">    <span class="meta">@Cacheable</span>(value = &#123;<span class="string">"users"</span>,<span class="string">"users2"</span>&#125;,key = <span class="string">"#id"</span>,condition = <span class="string">"#id=1"</span>)</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserEntity <span class="title">getUserById</span><span class="params">(Integer id)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        UserEntity userEntity = <span class="keyword">new</span> UserEntity();</span><br><span class="line">        userEntity.setId(id);</span><br><span class="line">        userEntity.setName(<span class="string">"张三"</span>);</span><br><span class="line">        userEntity.setAge(<span class="number">23</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"id值："</span>+id);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> userEntity;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 测试 ehcache 清楚缓存</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> userEntity</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@CacheEvict</span>(value = <span class="string">"users"</span>,allEntries = <span class="keyword">true</span>)</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">saveUser</span><span class="params">(UserEntity userEntity)</span></span>&#123;</span><br><span class="line">        System.out.println(userEntity);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 以下是key 实例</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Cacheable</span>(value=<span class="string">"users"</span>, key=<span class="string">"#id"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserEntity <span class="title">find1</span><span class="params">(Integer id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Cacheable</span>(value=<span class="string">"users"</span>, key=<span class="string">"#p0"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserEntity <span class="title">find2</span><span class="params">(Integer id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Cacheable</span>(value=<span class="string">"users"</span>, key=<span class="string">"#user.id"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserEntity <span class="title">find3</span><span class="params">(UserEntity user)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Cacheable</span>(value=<span class="string">"users"</span>, key=<span class="string">"#p0.id"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserEntity <span class="title">find4</span><span class="params">(UserEntity user)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 以下是模拟测试</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> typeId</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Cacheable</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">save</span><span class="params">(String typeId)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"save()执行了============="</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"模拟数据库保存"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@CachePut</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">update</span><span class="params">(String typeId)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"update()执行了============="</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"模拟数据库更新"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@CacheEvict</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">delete</span><span class="params">(String typeId)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"delete()执行了============="</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"模拟数据库删除"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Cacheable</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">select</span><span class="params">(String typeId)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"select()执行了============="</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"模拟数据库查询"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 复杂的缓存：高速缓存</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> name</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Caching</span>(</span><br><span class="line">            cacheable = &#123;</span><br><span class="line">                    <span class="meta">@Cacheable</span>(cacheNames = &#123;<span class="string">"users"</span>&#125;,key=<span class="string">"#name"</span>) <span class="comment">//（1）根据name查询user</span></span><br><span class="line">            &#125;,</span><br><span class="line">            put = &#123;</span><br><span class="line">                    <span class="meta">@CachePut</span>(cacheNames = &#123;<span class="string">"users"</span>&#125;,key=<span class="string">"#result.id"</span>) <span class="comment">//(2) 根据id查询user 以另一种key将查询出的结果缓存到缓存中</span></span><br><span class="line">            &#125;</span><br><span class="line">    )</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserEntity <span class="title">selectByName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        UserEntity userEntity = <span class="keyword">new</span> UserEntity();</span><br><span class="line">        userEntity.setId(<span class="number">1</span>);</span><br><span class="line">        userEntity.setName(name);</span><br><span class="line">        userEntity.setAge(<span class="number">23</span>);</span><br><span class="line">        System.out.println(<span class="string">"进入方法："</span>+name);</span><br><span class="line">        <span class="keyword">return</span> userEntity;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><div class="popular-posts-header"><p style="font-size:18px;color:red;margin:0 0 5px 0">相关文章推荐：</p></div><ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\1847461252.html" rel="bookmark">springboot+redis项目实战完整篇</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\379612345.html" rel="bookmark">SpringBoot配置-Hibernate-Validate</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\1127863787.html" rel="bookmark">springboot+Dozer+maven测试及项目实战演示</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\200750558.html" rel="bookmark">springboot-Solr-史上最详细</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\236595351.html" rel="bookmark">史上最详细springBoot初建</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\3431840903.html" rel="bookmark">springboot-redis-session配置两种方式</a></div></li></ul><div><div style="text-align:center;color:#ccc;font-size:14px">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div></div><div class="reward-container"><div><p style="font-size:14px;color:#34495e;margin:0 0 5px 0">赞赏一下吧～ 还可以关注公众号订阅最新内容</p></div><button disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">打赏</button><div id="qr" style="display:none"><div style="display:inline-block"><img src="/my-images/WeChat.png" alt="宇宙小神特别萌 微信支付"><p>微信支付</p></div><div style="display:inline-block"><img src="/my-images/Alipay.png" alt="宇宙小神特别萌 支付宝"><p>支付宝</p></div></div></div><footer class="post-footer"><div class="post-tags"><a href="/tags/SpringBoot/" rel="tag"><i class="fa fa-tag"></i> SpringBoot</a></div><div class="post-nav"><div class="post-nav-item"><a href="/posts/379612345.html" rel="prev" title="SpringBoot配置-Hibernate-Validate"><i class="fa fa-chevron-left"></i> SpringBoot配置-Hibernate-Validate</a></div><div class="post-nav-item"><a href="/posts/515071078.html" rel="next" title="Windows-下-Zookeeper-单机模式和伪分布式模式安装">Windows-下-Zookeeper-单机模式和伪分布式模式安装 <i class="fa fa-chevron-right"></i></a></div></div></footer></article></div></div><div class="comments" id="valine-comments"></div><script>window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }</script></div><div class="toggle sidebar-toggle"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div><aside class="sidebar"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="post-toc-wrap sidebar-panel"><div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#应用场景："><span class="nav-text">应用场景：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-ehcache"><span class="nav-text">1.ehcache</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-redis"><span class="nav-text">2.redis</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-memcached"><span class="nav-text">3.memcached</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-总结"><span class="nav-text">4.总结</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-仅仅演示springboot-ehcache代码："><span class="nav-text">5.仅仅演示springboot+ehcache代码：</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1、ehcache依赖："><span class="nav-text">1、ehcache依赖：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2、创建ehcache-xml文件"><span class="nav-text">2、创建ehcache.xml文件</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3、参考ehcache-xml配置文件详解"><span class="nav-text">3、参考ehcache.xml配置文件详解</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4、添加启用缓存"><span class="nav-text">4、添加启用缓存</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5、EhcacheController-java"><span class="nav-text">5、EhcacheController.java</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#6、接口UserService-java"><span class="nav-text">6、接口UserService.java</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#7、实现类UserServiceImpl-java"><span class="nav-text">7、实现类UserServiceImpl.java</span></a></li></ol></li></ol></li></ol></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="宇宙小神特别萌" src="/my-images/avatar.gif"><p class="site-author-name" itemprop="name">宇宙小神特别萌</p><div class="site-description" itemprop="description">人生就是一次幸运的旅行！</div></div><div class="site-state-wrap motion-element"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">116</span> <span class="site-state-item-name">文章</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">27</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://github.com/zhengjiaao" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhengjiaao" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a> </span><span class="links-of-author-item"><a href="https://www.jianshu.com/u/70d69269bd09" title="简书 → https:&#x2F;&#x2F;www.jianshu.com&#x2F;u&#x2F;70d69269bd09" rel="noopener" target="_blank"><i class="fa fa-fw fa-book"></i>简书</a> </span><span class="links-of-author-item"><a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i>RSS</a></span></div><div class="links-of-blogroll motion-element"><div class="links-of-blogroll-title"><i class="fa fa-fw fa-link"></i> 推荐阅读</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="https://github.com/spring-projects" title="https:&#x2F;&#x2F;github.com&#x2F;spring-projects" rel="noopener" target="_blank">spring-projects</a></li><li class="links-of-blogroll-item"><a href="http://www.alloyteam.com/nav/" title="http:&#x2F;&#x2F;www.alloyteam.com&#x2F;nav&#x2F;" rel="noopener" target="_blank">Web前端导航</a></li><li class="links-of-blogroll-item"><a href="http://www.chuangzaoshi.com/code" title="http:&#x2F;&#x2F;www.chuangzaoshi.com&#x2F;code" rel="noopener" target="_blank">创造狮导航</a></li></ul></div></div></div></aside><div id="sidebar-dimmer"></div></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">&copy; <span itemprop="copyrightYear">2020</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">宇宙小神特别萌</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-area-chart"></i> </span><span title="站点总字数">1.2m</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-coffee"></i> </span><span title="站点阅读时长">17:41</span></div><div class="busuanzi-count"><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span class="post-meta-item" id="busuanzi_container_site_uv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span> </span></span><span class="post-meta-divider">|</span> <span class="post-meta-item" id="busuanzi_container_site_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span id="busuanzi_value_site_pv"></span></span></span></div></div></footer></div><script src="//cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js"></script><script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script><script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.ui.min.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script>!function(){var t=document.createElement("script"),e=window.location.protocol.split(":")[0];t.src="https"===e?"https://zz.bdstatic.com/linksubmit/push.js":"http://push.zhanzhang.baidu.com/push.js";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(t,s)}()</script><script src="/js/local-search.js"></script><script>NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//cdnjs.cloudflare.com/ajax/libs/valine/1.3.10/Valine.min.js', () => {
    <!--var GUEST = ['nick', 'mail', 'link'];-->
    var GUEST = ['nick', 'mail'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el: '#valine-comments',
      verify: false,
      notify: false,
      appId: 'OuyHeA0XGL8hK4vgbqL0iERx-gzGzoHsz',
      appKey: 'L58VglKmWULcGK2W2AN7kWzM',
      placeholder: "留下邮箱，有回复时你将收到提醒，邮箱不会被公开。",
      avatar: 'wavatar',
      meta: guest,
      pageSize: '10' || 10,
      visitor: true,
      lang: 'zh-cn' || 'zh-cn',
      path: location.pathname,
      recordIP: true,
      serverURLs: ''
    });
  }, window.Valine);
});</script><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({pluginRootPath:"live2dw/",pluginJsPath:"lib/",pluginModelPath:"assets/",tagMode:!1,model:{jsonPath:"/live2dw/assets/wanko.model.json"},display:{position:"right",width:150,height:300},mobile:{show:!1},log:!1})</script></body><script type="text/javascript" src="/js/src/activetitle.js"></script></html><!-- rebuild by neat -->